package org.istt.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
 * Group entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "TBL_GROUP", schema = "dbo", catalog = "onegate")
public class Group extends BaseObject<Long> implements java.io.Serializable {

	private static final long serialVersionUID = -282642574085273676L;
	private Long id;
	private String name;
	private String authority;
	private String description;
	private Boolean roleSystem;
	private Set<Menu> menus = new HashSet<Menu>(0);
	private Set<User> users = new HashSet<User>(0);

	// Constructors

	/** default constructor */
	public Group() {
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "ID", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "NAME")
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "DESCRIPTION")
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	
	@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
	@JoinTable(name = "TBL_MENU_GROUP", schema = "dbo", catalog = "onegate", joinColumns = { @JoinColumn(name = "GROUP_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "MENU_ID", nullable = false, updatable = false) })
	public Set<Menu> getMenus() {
		return this.menus;
	}

	public void setMenus(Set<Menu> menus) {
		this.menus = menus;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "groups")
	public Set<User> getUsers() {
		return this.users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	@Column(name = "AUTHORITY")
	public String getAuthority() {
		return authority;
	}

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	@Override
	public int hashCode() {
		/*int result;
		result = getName().hashCode();
		result = 29 * result + getAuthority().hashCode();
		return result;*/
		
		return 0;
	}

	@Override
	public String toString() {
		return new StringBuilder("id = ").append(id)
				.append("name = ").append(name)
				.append("authority = ").append(authority)
				.append("description = ").append(description)
				.toString();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof Group)) return false;

		final Group ps = (Group) o;


        if (!getName().equals(ps.getName())) return false;
        if (!getAuthority().equals(ps.getAuthority())) return false;

		return true;
	}

	public void setRoleSystem(Boolean roleSystem) {
		this.roleSystem = roleSystem;
	}
	@Column(name = "IS_SYSTEM")
	public Boolean getRoleSystem() {
		return roleSystem;
	}

}